package com.situ.mvc.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.dao.IUserDao;
import com.situ.mvc.dao.impl.UserDaoImpl;
import com.situ.mvc.entity.Department;
import com.situ.mvc.entity.User;
import com.situ.mvc.service.IUserService;

import sun.awt.RepaintArea;

public class UserServiceImpl implements IUserService {
	
	private IUserDao userDao = new UserDaoImpl();

	@Override
	public ServerResponse login(String name, String password) {
		int count = userDao.login(name,password);
		if (count <= 0) {
			return ServerResponse.createError("登陆失败");
		}
		return ServerResponse.createSuccess("登陆成功");
	}

	@Override
	public ServerResponse findList(Integer page,Integer limit) {
		int count = userDao.findCount();
		int offset = (page-1)*limit;
		List<Map<String, Object>> data = userDao.getList(offset,limit);
		if (data == null) {
			return ServerResponse.createError("获取失败");
		}
		return ServerResponse.createSuccess("获取成功", count, data);
	}

	@Override
	public ServerResponse getSelect() {
		List<Department> data = userDao.findSelect();
		if (data == null) {
			return ServerResponse.createError("失败");
		}
		return ServerResponse.createSuccess("成功", data);
	}

	@Override
	public ServerResponse addUser(User user) {
		int count = userDao.addUser(user);
		if (count <= 0) {
			return ServerResponse.createError("添加失败");
		}
		return ServerResponse.createSuccess("添加成功");
	}

	@Override
	public ServerResponse deleteAll(String ids) {
		String[] array = ids.split(",");
		for (String id : array) {
			userDao.deleteById(Integer.parseInt(id));
		}
		return ServerResponse.createSuccess("批量删除成功");
	}

	@Override
	public ServerResponse deleteById(Integer id) {
		int count = userDao.deleteById(id);
		if (count <= 0) {
			return ServerResponse.createError("删除失败");
		}
		return ServerResponse.createSuccess("删除成功");
	}

	@Override
	public User toUpdate(Integer id) {
		return userDao.toUpdate(id);
	}

	@Override
	public ServerResponse getDepartmentList() {
		int count = userDao.findDepartmentCount();
		List<Department> data = userDao.getDepartmentList();
		if (data == null) {
			return ServerResponse.createError("获取失败");
		}
		return ServerResponse.createSuccess("获取成功", count, data);
	}

	@Override
	public ServerResponse deleteByDepartmentId(Integer id) {
		int count = userDao.deleteByDepartmentId(id);
		if (count <= 0) {
			return ServerResponse.createError("删除失败");
		}
		return ServerResponse.createSuccess("删除成功");
	}

	@Override
	public Department getUpdateDepartmentPage(Integer id) {
		Department department = userDao.getUpdateDepartmentPage(id);
		return department;
	}

	@Override
	public ServerResponse userUpdate(User user) {
		int count = userDao.userUpdate(user);
		if (count <= 0) {
			return ServerResponse.createError("修改失败");
		}
		return ServerResponse.createSuccess("删除成功");
	}

}
